#include "Paitent.h"
Paitent::Paitent()
{
	InitResistance();
	DoStart();
}
void Paitent::InitResistance()
{
	this->m_resistance = rand()%9000 + 1000;

}
void Paitent::DoStart()
{
	int sizeOfVirus;
	sizeOfVirus = rand()%20 + 10;
	for(int i = 0; i < sizeOfVirus; i ++)
	{
		if(i%2)
		{
			Virus* fluVirus = new FluVirus();
			this->m_virusList.push_back(fluVirus);
		}
		else
		{
			Virus* dengueVirus = new DengueVirus();
			this->m_virusList.push_back(dengueVirus);
		}
	}
	this->m_state = 1;
}
void Paitent::TakeMedicine(int medicine_resistance)
{
	int totalVirusResistance = 0;
	list<Virus*>::const_iterator i = m_virusList.begin();
	list<Virus*>::const_iterator end = m_virusList.end();
	list<Virus*>::const_iterator itemp ;
	//Check 
	for (list<Virus*>::const_iterator i = m_virusList.begin(); i != m_virusList.end(); ++i)
	{

		totalVirusResistance += (*i)->m_resistance;
	}
	
	if(this->m_resistance <= totalVirusResistance)
	{
		
		this->DoDie();
	}
	while(i != end)
	{
		itemp = i;
		++itemp;
		(*i)->ReduceResistance(medicine_resistance);
		if((*i)->m_resistance <= 0)
		{
			Virus* p = (*i);
			m_virusList.erase(i);
			delete p;
			//delete (*i);
		}
		else 
		{
			Virus* vr = (*i)->DoClone();
			int temp = (*i)->GetType();
			if(temp == DENGUEVIRUS)
			{
		
				m_virusList.push_front(vr);
				Virus *vr1 = vr->DoClone();
				m_virusList.push_front(vr1);
			}
			else if(temp == FLUVIRUS)
			{
				m_virusList.push_back(vr);
			}
			
		}
		i = itemp;
	}
	
	
}
void Paitent::DoDie()
{
	m_state = 0;
	for (list<Virus*>::const_iterator i = m_virusList.begin(); i != m_virusList.end(); ++i)
	{
		delete (*i);
	}
	m_virusList.clear();
	
}
int Paitent::GetState()
{
	return this->m_state;
}